import { save, pages, LoadFrom, Delete } from '@/api/job/jobCategory'
// import { DepartmentTree } from '@/api/systems/systemDepartment'

const state = {
  controllerName: 'jobCategory',
  dataList: {
    listLoading: false,
    list: [],
    departmentTree: [],
    defaultProps: {
      children: 'departmentList',
      key: 'id',
      label: 'name'
    },
    searchForm: {
      CategoryName: '',
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0,
      ParentId: '00000000-0000-0000-0000-000000000000'
    }
  },
  treeData: {
    List: [],
    props: {
      label: 'label',
      children: 'children',
      isLeaf: 'leaf'
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: {},
    ResetPwdFormState: false,
    ResetPwdModel: {}
  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`job/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`job/${state.controllerName}/findList`)
  }
}

const actions = {
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    LoadFrom({ ID: par })
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.model = Data
        _state.form.formState = true
      })
      .catch(error => {
        console.log(error)
      })
  },
  loadTree(context, par) {
    // var _state = context.
    // node, resolve
    console.log('loadTreeloadTreeloadTreeloadTreeloadTree', context, par)
    var searchTree = { PageSize: 1000, pageIndex: 1, ParentId: '00000000-0000-0000-0000-000000000000' }
    if (par.node.level > 0) {
      searchTree = { PageSize: 1000, pageIndex: 1, ParentId: par.node.data.id }
    }
    pages(searchTree)
      .then(res => {
        const { Data } = res
        console.log(Data, 'DataloadTree')
        var treeNode = []
        Data.List.forEach(item => {
          treeNode.push({
            id: item.ID,
            label: item.CategoryName,
            children: [],
            leaf: false
          })
        })
        return par.resolve(treeNode)
      })
      .catch(error => {
        console.log(error)
      })
  },
  loadNode(context, par) {
    var _state = context.state
    console.log('loadNode', par)
    _state.dataList.searchForm.pageIndex = 1

    _state.dataList.searchForm.ParentId = par.id
    context.dispatch('findList')
  },
  BaseNode(context) {
    var _state = context.state
    _state.dataList.searchForm.pageIndex = 1
    _state.dataList.searchForm.ParentId = '00000000-0000-0000-0000-000000000000'
    context.dispatch('findList')
  },
  save(context, par) {
    var _state = context.state
    par.ParentId = _state.dataList.searchForm.ParentId
    save(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        global.tools.msg('保存成功!', '成功')
        _state.form.formState = false

        context.dispatch('findList')
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.PageIndex
        _state.dataList.searchForm.pageSize = Data.PageSize
        _state.dataList.searchForm.dataCount = Data.Count
        _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  delete(context, par) {
    console.log(par)
    global.tools.confirm(
      '确定要删除吗？',
      function() {
        Delete({
          id: par
        })
          .then(res => {
            const { Data } = res
            console.log(Data)
            global.tools.msg('删除成功!', '成功')
            context.dispatch('findList')
          })
          .catch(e => {
            console.log(e)
          })
      },
      null,
      '提示'
    )
  },
  restPwdFrom(context, par) {
    var _state = context.state
    _state.form.ResetPwdFormState = true
    _state.form.ResetPwdModel = {
      Id: par,
      Password: '',
      NewPassword: ''
    }
  },

  handleNodeClick(context, node) {
    var _state = context.state
    _state.form.model.departmentId = node.id
    _state.form.model.departmentName = node.name
    _state.form.formDepartmentState = false
  },
  handleSuccess(context, res) {
    console.log(res)
    if (res.Status === 1) {
      var _state = context.state
      _state.form.model.CompanyLogo = res.Data.Url
      console.log('_state.form.model.CompanyLogo', _state.form.model.CompanyLogo)
    } else {
      global.tools.msg('上传失败，请重新上传！', '提示')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
